/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.guice.transaction;

import javax.persistence.EntityManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * EntityManager Transaction Wrapper Provider.
 * @author Adam
 * @version $Id: DefaultTransactionProvider.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Oct 9, 2008 at 1:00:23 PM 
 */
@Singleton
public class DefaultTransactionProvider implements Provider<Transaction> {
	/**
	 * Entity Manager Provider, used to get the Entity Manager.
	 */
	private final Provider<EntityManager> provider;

	/**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private static final Logger LOG = LoggerFactory.getLogger(DefaultTransactionProvider.class);

    /**
	 * Constructs a EntityMananger Transaction Provider.
	 * @param provider the Entity Manager provider.
	 */
	@Inject
	public DefaultTransactionProvider(final Provider<EntityManager> provider) {
		if (provider == null) {
			throw new IllegalArgumentException("Entity Mananger cannot be null");
		}
		this.provider = provider;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Provider#get()
	 */
	public Transaction get() {
		LOG.debug("Creating a new transaction");
		return new DefaultTransaction(provider.get());
	}
}
